package com.example.restatr.Atcivity.Hospital;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.ContentAt;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Hospital.Bean.HospitalBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HospitalAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private Banner rbt;
    private RecyclerView rv;
    private List<HospitalBean.RowsBean> rowsBeanList = new ArrayList<>();
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.hospital_layout );

        toolbar = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        rbt = findViewById( R.id.banner );
        rv = findViewById( R.id.rv );

        Tools.SetBarTitle( toolbar, barTitle, "门诊预约", search, this, false );
        setBanner();
        GetHospital();
        intent = new Intent( HospitalAt.this, HospitalPatientAt.class );

    }

    private void setBanner() {
        List<String> imgUrls = new ArrayList<>();
        ImageLoader imageLoader = new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with( context )
                        .load( Tools.ip + o )
                        .transform( new RoundedCorners( 15 ) )
                        .into( imageView );
            }
        };
        new Thread( () -> {
            try {
                String get = Tools.Get( "/prod-api/api/hospital/banner/list", MainActivity.sp.getString( "token", "" ) );
                JSONObject jx = new JSONObject( get );
                JSONArray rows = jx.getJSONArray( "data" );
                for (int i = 0; i < rows.length(); i++) {
                    JSONObject all = rows.getJSONObject( i );
                    imgUrls.add( all.getString( "imgUrl" ) );
                }
                runOnUiThread( new Runnable() {
                    @Override
                    public void run() {
                        rbt.setBannerStyle( BannerConfig.NUM_INDICATOR )
                                .setImageLoader( imageLoader )
                                .setImages( imgUrls )
                                .setIndicatorGravity( BannerConfig.CENTER )
                                .start();
                    }
                } );
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } ).start();
    }

    private void GetHospital() {
        new Thread( () -> {
            String get = Tools.Get( "/prod-api/api/hospital/hospital/list", "" );
            HospitalBean hospitalBean = new Gson().fromJson( get, HospitalBean.class );
            rowsBeanList.addAll( hospitalBean.getRows() );
            if (rowsBeanList.size() > 0) {
                runOnUiThread( () -> {
                    rv.setAdapter( new adapter() );
                } );
            }
        } ).start();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jiuzhen:
                intent.putExtra( "intent", "就诊" );
                break;
            case R.id.yuyue:
                intent.putExtra( "intent", "预约" );
                break;
        }
        startActivity( intent );
    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate( R.layout.hospital_item_layout, parent, false );
            return new viewHolder( inflate );
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            Glide.with( HospitalAt.this )
                    .load( Tools.ip + rowsBeanList.get( position ).getImgUrl() )
                    .transform( new RoundedCorners( 15 ) )
                    .into( holder.img );
            holder.name.setText( rowsBeanList.get( position ).getHospitalName() );
            holder.rating.setRating( rowsBeanList.get( position ).getLevel() );
            holder.itemView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( HospitalAt.this, ContentAt.class );
                    intent.putExtra( "content", rowsBeanList.get( position ).getBrief() );
                    intent.putExtra( "title", rowsBeanList.get( position ).getHospitalName() );
                    intent.putExtra( "type", "default" );
                    startActivity( intent );
                }
            } );
        }

        @Override
        public int getItemCount() {
            return rowsBeanList.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private ImageView img;
            private TextView name;
            private RatingBar rating;

            public viewHolder(@NonNull View itemView) {
                super( itemView );
                img = itemView.findViewById( R.id.img );
                name = itemView.findViewById( R.id.name );
                rating = itemView.findViewById( R.id.rating );
            }
        }
    }
}
